package com.zhhs.galaxyott.httpVolley.parser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;
import android.util.Log;

import com.zhhs.galaxyott.beans.HotMoviesBean;
import com.zhhs.galaxyott.httpVolley.response.HotMoviesResponse;

public class HotMoviesParser extends BaseParser<HotMoviesResponse> {

    @Override
    public HotMoviesResponse parse(String jsonString) {
        HotMoviesResponse response = new HotMoviesResponse();

        try {
            // 如果用fastJoson就是此处不同
            // JSONObject obj = JSON.parseObject(paramString);
            JSONObject obj = new JSONObject(jsonString);
            parseMsg(obj, response);

            // 先判断返回码.
            int errCode = obj.getInt("errCode");
            response.errCode = errCode;
            String returnCode = String.valueOf(errCode);

            if (TextUtils.isEmpty(returnCode)) {
                return null;
            }
            Log.i("DEBUG_LC", "THIS IS returnCode != 0");
            if (returnCode.equals("0")) {// 返回正常.

                JSONObject pageBean = obj.getJSONObject("pageBean");

                JSONArray recordList = pageBean.getJSONArray("recordList");
                Log.i("DEBUG_LC", "THIS IS returnCode = 0" + recordList.length());
                for (int i = 0; i < recordList.length(); i++) {
                    HotMoviesBean Bean = new HotMoviesBean();
                    JSONObject jo = recordList.getJSONObject(i);
                    Bean.setId(jo.optInt("id"));
                    Bean.setIdentify(jo.optString("identify"));
                    Bean.setImgurl(jo.optString("imgurl"));
                    Bean.setName(jo.optString("name"));
                    Bean.setEname(jo.optString("ename"));
                    Bean.setDirector(jo.optString("director"));
                    Bean.setActor(jo.optString("actor"));
                    Bean.setWatchNum(jo.optInt("watchNum"));
                    // Bean.setMovieSort(jo.optInt("movieSort"));
                    if (jo.getString("categoryName") == null) {
                        Bean.setCategoryName("null");
                    } else {
                        Bean.setCategoryName(jo.optString("categoryName"));
                    }

                    response.Beans.add(Bean);
                }

                JSONObject special = obj.getJSONObject("special");

                response.setSpecialTitle(special.optString("specialTitle"));
                response.setImageUrl(special.optString("imageUrl"));

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return response;
    }

}
